document.getElementById('submit-post').addEventListener('click', () => {
    const url = '/api/v1/submit-post'
    const title = document.getElementById('post-title').value;
    const content = document.getElementById('post-content').value;
    const token = localStorage.getItem('jwt_token')
    const data = {
        title: title,
        content: content
    }
    fetch(url, {
        method: "POST",
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${token}`
        },
        body:JSON.stringify(data)
    })
    .then(response => {
        if(!response.ok) {
            if (response.status == 400) {
                throw new Error('参数错误');
            } else {
                throw new Error(`未知错误，状态码：${response.status}`);
            }
        }
        return response.json();
    })
    .then(result => {
        console.log(result)
        alert(result);
        window.open('/index.html', '_self');
    })
    .catch(error => {
        alert(error)
    })
})